<template>
  <div>
    <a-modal
      centered
      :title="title"
      :width="1200"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭">

        <!-- 操作按钮区域 -->
        <div class="table-operator">
            <!-- 高级查询区域 -->
            <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
        </div>

        <!-- table区域-begin -->
        <div>
            <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
                <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
                <a style="margin-left: 24px" @click="onClearSelected">清空</a>
            </div>

            <a-table
                ref="table"
                size="middle"
                :scroll="{x:true}"
                bordered
                rowKey="id"
                :columns="columns"
                :dataSource="dataSource"
                :pagination="ipagination"
                :loading="loading"
                :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
                class="j-table-force-nowrap"
                @change="handleTableChange">
            </a-table>
        </div>
    </a-modal>
  </div>
</template>

<script>
    import {filterObj} from '@/utils/util'
    import {getAction} from '@/api/manage'
    import '@/assets/less/TableExpand.less'

  export default {
    props:{
        radid: {
            type: String,
            default: ''
        },
    },
    name: "SelectRoomOfOneModel",
    data() {
      return {
        title: "添加房源",
        visible: false,
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'房源号',
            align:"center",
            dataIndex: 'no'
          },
          {
            title:'产权证号',
            align:"center",
            dataIndex: 'propertyNo'
          },
          {
            title:'班组名称',
            align:"center",
            dataIndex: 'groupName'
          },
          {
            title:'单元',
            align:"center",
            dataIndex: 'unit'
          },
          {
            title:'门牌号',
            align:"center",
            dataIndex: 'houseNo'
          },
          {
            title:'楼层',
            align:"center",
            dataIndex: 'floor'
          },
          {
            title:'面积',
            align:"center",
            dataIndex: 'area'
          },
          {
            title:'建造年月',
            align:"center",
            dataIndex: 'buildTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'收费标准',
            align:"center",
            dataIndex: 'fare'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'state_dictText'
          },
        ],
        
        
        dataSource: [],
        // 分页参数
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        isorter: {
          column: 'createTime',
          order: 'desc',
        },
        loading: false,
        selectedRowKeys: [],
        selectedRows: [],

        dictOptions:{},
        superFieldList:[],
        queryParam:{},

        /* 高级查询条件生效状态 */
        superQueryFlag:false,
        /* 高级查询条件 */
        superQueryParams: '',
        /** 高级查询拼接方式 */
        superQueryMatchType: 'and',

        url: {
          listbyCheckDepartid: "/roomapartdistribute/znzfCheckApartRoomList/znzfApplyToZnzfRoomApartDistribute",
        },
      }
    },
    created() {
      //this.loadData();
      this.getSuperFieldList();
    },
    methods: {
        initDictConfig(){
        },
        handleSuperQuery(params, matchType) {
          //高级查询方法
          if(!params){
            this.superQueryParams=''
            this.superQueryFlag = false
          }else{
            this.superQueryFlag = true
            this.superQueryParams=JSON.stringify(params)
            this.superQueryMatchType = matchType
          }
          this.loadData(1)
        },
        getSuperFieldList(){
        let fieldList=[];
            fieldList.push({type:'string',value:'no',text:'房源号'})
            fieldList.push({type:'string',value:'propertyNo',text:'产权证号'})
            fieldList.push({type:'string',value:'houseNo',text:'门牌号'})
            fieldList.push({type:'string',value:'groupName',text:'班组名称'})
            fieldList.push({type:'string',value:'unit',text:'单元'})
            fieldList.push({type:'int',value:'floor',text:'楼层'})
            fieldList.push({type:'string',value:'roomNo',text:'房间号'})
            fieldList.push({type:'number',value:'area',text:'面积'})
            fieldList.push({type:'date',value:'buildTime',text:'建造年月'})
            fieldList.push({type:'number',value:'fare',text:'收费标准'})
            fieldList.push({type:'int',value:'state',text:'状态'})
            this.superFieldList = fieldList
        },
        getQueryField() {
            //TODO 字段权限控制
            var str = "id,";
            this.columns.forEach(function (value) {
                str += "," + value.dataIndex;
            });
            return str;
        },
        getQueryParams() {
            //获取查询条件
            let sqp = {}
            if(this.superQueryParams){
                sqp['superQueryParams']=encodeURI(this.superQueryParams)
                sqp['superQueryMatchType'] = this.superQueryMatchType
            }
            var param = Object.assign(sqp, this.queryParam, this.isorter ,this.filters);
            param.field = this.getQueryField();
            param.pageNo = this.ipagination.current;
            param.pageSize = this.ipagination.pageSize;
            return filterObj(param);
        },
        loadData(arg){
            
            if (!this.url.listbyCheckDepartid) {
                this.$message.error('请设置url.listbyCheckDepartid属性!')
                return
            }
            if (this.radid === ''){
                this.$message.error('未指定项目!')
                return
            }
            if (arg === 1) {
                this.ipagination.current = 1
            }
        
            let params = this.getQueryParams()//查询条件
            params.radId = this.radid 

            this.loading = true

            getAction(this.url.listbyCheckDepartid, params).then((res) => {
                if (res.success) {
                    this.dataSource = res.result.records;
                    if(res.result.total)
                    {
                        this.ipagination.total = res.result.total;
                    }else{
                        this.ipagination.total = 0;
                    }
                }else{
                    this.dataSource = [];
                    this.ipagination.total = 0;
                    this.$message.warning(res.message)
                }
            }).finally(() => {
                this.loading = false
            })
        },
      

        onSelectChange(selectedRowKeys, selectionRows) {
          this.selectedRowKeys = selectedRowKeys
          this.selectionRows = selectionRows
        },
        onClearSelected(){
          this.selectedRowKeys = []
          this.selectionRows = []
        },
        handleTableChange(pagination, filters, sorter) {
          //分页、排序、筛选变化时触发
          //TODO 筛选
          if (Object.keys(sorter).length > 0) {
            this.isorter.column = sorter.field
            this.isorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
          }
          this.ipagination = pagination
          this.loadData()
        },

        handleOk() {
            console.log("data:" + this.selectedRowKeys);
            this.$emit("selectFinished", this.selectedRowKeys);
            this.visible = false;
        },
        handleCancel(){
            this.visible = false;
        },
        openThisModel(hasDistributeRoomList){
          this.loadData();
          this.selectedRowKeys = hasDistributeRoomList;
          this.visible = true;
        }

    }
  }
</script>
<style lang="less" scoped>
  .ant-card-body .table-operator {
    margin-bottom: 18px;
  }

  .ant-table-tbody .ant-table-row td {
    padding-top: 15px;
    padding-bottom: 15px;
  }

  .anty-row-operator button {
    margin: 0 5px
  }

  .ant-btn-danger {
    background-color: #ffffff
  }

  .ant-modal-cust-warp {
    height: 100%
  }

  .ant-modal-cust-warp .ant-modal-body {
    height: calc(100% - 110px) !important;
    overflow-y: auto
  }

  .ant-modal-cust-warp .ant-modal-content {
    height: 90% !important;
    overflow-y: hidden
  }
</style>